import router from './router'
import nprogress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import store from './store'

// 白名单
const white_list = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  nprogress.start()
  const token = store.getters.token
  // next()
  if (token) {
    if (to.path === '/login') {
      nprogress.done()
      next('/')
    } else {
      if (!store.getters.userId) {
        await store.dispatch('user/getUserInofAction')
      }
      next()
    }
  } else {
    // token不存在
    // next('/login')
    if (white_list.includes(to.path)) {
      next()
    } else {
      nprogress.done()
      next('/login')
    }
  }
})
router.afterEach(route => {
  nprogress.done()
})
